/**
 * 一键参团页面
 * author : weiki
 * date : 2017/09/27
 * alter: 2017/10/09
 */


var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    state:2,             //拼团状态
    urlImage: app.urlimg,//图片url前缀   
    orderInfo : {},      //商品信息
    countdown: "",//倒计时
    milliseconds: 2000,      //倒计时秒钟数
    headIco : [],         //头像
    readFinish: false,    //是否加载完成
    orderId: 0,
    teamSign: 0,
    regimentId:0,
  },

  /**
   * 倒计时
   */
  time: function () {
      var that = this;

      var Time = that.data.milliseconds-=1000;

      if (Time > 0) {
          var hours = parseInt(Time / 1000 / 60 / 60 % 24, 10); //计算剩余的小时 
          if (('' + hours).length == 1) {
              hours = '0' + hours;
          }
          var minutes = parseInt(Time / 1000 / 60 % 60, 10);//计算剩余的分钟 
          if (('' + minutes).length == 1) {
              minutes = '0' + minutes;
          }
          var seconds = parseInt(Time / 1000 % 60, 10);//计算剩余的秒数 
          if (('' + seconds).length == 1) {
              seconds = '0' + seconds;
          }
          var times = hours + ":" + minutes + ":" + seconds;//排列时间

          that.setData({
              countdown: times,
          })
      }
  },

  /**
   * 一键参团
   */
  join: function () {
      var goodsId = this.data.orderInfo.goods[0]['goods_id'];
      wx.redirectTo({
          url: '/pages/group/group_of/group_of?id='+goodsId+
          '&order_id=' + this.data.orderId + '&teamSign=' + this.data.teamSign + '&regimentId=' + this.data.regimentId,
      })
  },

  /**
   * 重新开团
   */
  renew: function () {
      wx.redirectTo({
          url: '/pages/group/groupdetail/index?id=1000',
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
        state : options.state,
        teamSign: options.teamSign,
        orderId: options.orderId,
        regimentId: options.regimentId
    });
    wx.showLoading({
        title: '加载中...',
    });
    var that = this;
    wx.request({
        url: app.url + "api_team/inviteGroup",
        data: {
            orderId: options.orderId
        },
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token_key')
        },
        success: function (res) {
            
            var headIco = res.data.result.needPeople;
            var headArr = [];
            for (var i=0;i<headIco;i++) {
                headArr[i] = i;
            }
            that.setData({
                readFinish: true,
                orderInfo: res.data.result,
                state: res.data.result.teamStatus,
                milliseconds: res.data.result.surplusTime,
                headIco :headArr,
            });
        }
    });
    wx.hideLoading();
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var cutTime = setInterval(function () {
        if (that.data.milliseconds > 1000) {
            that.time();
        } else {
            that.setData({
                state: 2
            });
            clearInterval(cutTime);
        }

    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  
})